<template>
  <div class="Navbar">
    <div class="container">
      <div class="Nav-l">
        <nav
          id="nav-male"
          :class="{
            actived: maleActived,
          }"
          @click="toRoute('Male')"
        >
          男生阅读
        </nav>
        <nav
          id="nav-female"
          :class="{
            actived: !maleActived,
          }"
          @click="toRoute('Female')"
        >
          女生阅读
        </nav>
      </div>
      <div class="Nav-r">
        <nav>
          <input
            id="search"
            type="text"
            placeholder="搜索,然后回车"
            @keydown.enter="handleSearch"
            v-model="keyword"
          />
        </nav>
        <nav v-if="login">用户：{{ user }}</nav>
        <nav id="nav-login" @click="toLogin" v-if="!login">登录</nav>
        <nav id="nav-register" @click="toRegister" v-if="!login">注册</nav>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, defineEmits, onMounted, computed } from "vue";
import { useRouter } from "vue-router";
import { getNav } from "../../api/api.js";
const router = new useRouter();
let maleActived = ref(true);
let keyword = ref("");

let login = computed(() => {
  console.log(localStorage.getItem("token") == null);
  if (localStorage.getItem("token") == null) {
    return false;
  } else {
    return true;
  }
});

const user = computed(() => {
  return JSON.parse(localStorage.getItem("userInfo")).account;
});

let handleSearch = () => {
  router.push({
    path: "/home/search",
    query: {
      keyword: keyword.value,
    },
  });
  keyword.value = "";
};

let toRoute = (v) => {
  if (v == "Male") {
    router.push({
      path: "/home/male",
    });
    maleActived.value = true;
  } else {
    router.push({
      path: "/home/female",
    });
    maleActived.value = false;
  }
};

let toLogin = () => {
  router.push({
    path: "/login",
  });
};
let toRegister = () => {
  router.push({
    path: "/register",
  });
};

onMounted(() => {
  getNav();
});
</script>

<style scoped lang="scss">
.Navbar {
  background-color: #f7f7f7;
  border-bottom: 1px solid #e5e5e5;
  .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    background-color: #f7f7f7;
    nav {
      padding: 10px;
      cursor: pointer;
    }
    .actived {
      background-color: #fff;
      border-left: 1px solid #e5e5e5;
      border-right: 1px solid #e5e5e5;
    }
  }
  .Nav-l {
    display: flex;
  }
  .Nav-r {
    display: flex;
    justify-content: space-between;
    align-items: center;
    #nav-login,
    #nav-register {
      line-height: 40px;
    }
    nav {
      #search {
        padding: 5px 10px;
        border-radius: 5px;
        font-size: 12px;
        border: 1px solid #ccc;
        outline-color: skyblue;
      }
    }
  }
}
</style>
